package com.yantong.controller.locallife;

import com.yantong.controller.BaseController;
import com.yantong.pojo.ListMemberParams;
import com.yantong.pojo.Member;
import com.yantong.pojo.Store;
import com.yantong.pojo.StoreFreecharge;
import com.yantong.result.ResultVo;
import com.yantong.service.CommanderListService;
import com.yantong.service.StoreFreeChargeService;
import com.yantong.service.StoreService;
import com.yantong.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/store/freecharge")
public class StoreFreeChargeController extends BaseController {

    private static final String prefix = "store/freecharge";
    @Autowired
    private StoreFreeChargeService storeFreeChargeService;
    @Autowired
    private CommanderListService commanderListService;
    @Autowired
    private StoreService storeService;

    @ResponseBody
    @GetMapping("/list")
    @RequiresPermissions("store:freecharge:list")
    public PageUtils<StoreFreecharge> listStoreFreeCharge(@RequestParam Map<String,Object> params) {
        return storeFreeChargeService.list(params);
    }

    /**
     * 大礼包编辑页面
     * @param freeChargeId
     * @param model
     * @return
     */
    @GetMapping("/editFreeCharge/{freeChargeId}")
   //@RequiresPermissions("commanderList:manage:edit")
    public String editFreeCharge(@PathVariable Long freeChargeId, Model model) {
       StoreFreecharge storeFreecharge =  storeFreeChargeService.selectByPrimaryKey(freeChargeId);
        model.addAttribute("freeCharge", storeFreecharge);
        return prefix + "/freechargeedit";
    }



    @ResponseBody
    @PostMapping("/saveFreechargeDesc")
    //@RequiresPermissions("store:freecharge:add")
    public ResultVo saveFreechargeDesc(@RequestBody Store storeFreecharge) {
        ResultVo resultVo = ResultVo.ok();
        storeService.updateFreeChargeDescByTeamId(storeFreecharge);
        return resultVo;
    }

    @ResponseBody
    @PostMapping("/add")
    //@RequiresPermissions("store:freecharge:add")
    public ResultVo addStoreFreeCharge(@RequestBody StoreFreecharge storeFreecharge) {
        ResultVo resultVo = ResultVo.ok();
        storeFreeChargeService.addStoreFreeCharge(storeFreecharge);
        return resultVo;
    }

    @ResponseBody
    @PostMapping("/edit")
    //@RequiresPermissions("store:freecharge:edit")
    public ResultVo editStoreFreeCharge(@RequestBody StoreFreecharge storeFreecharge) {
        ResultVo resultVo = ResultVo.ok();
        storeFreeChargeService.editStoreFreeCharge(storeFreecharge);
        return resultVo;
    }

    @ResponseBody
    @PostMapping(value = "/update", produces="application/json; utf-8")
    public ResultVo update(StoreFreecharge storeFreecharge) {
        if (storeFreecharge == null || storeFreecharge.getId() == null) {
            return ResultVo.error();
        }
        StoreFreecharge storeFreechargeOld = storeFreeChargeService.selectByPrimaryKey(storeFreecharge.getId());
        storeFreecharge.setCentreId(storeFreechargeOld.getCentreId());
        storeFreecharge.setTeamId(storeFreechargeOld.getTeamId());
        if (storeFreeChargeService.editStoreFreeCharge(storeFreecharge) < 1) {
            return ResultVo.error();
        }
        return ResultVo.ok();
    }

    @ResponseBody
    @PostMapping("/delete")
    //@RequiresPermissions("store:freecharge:delete")
    public ResultVo deleteStoreFreeCharge(@RequestBody StoreFreecharge storeFreecharge) {
        ResultVo resultVo = ResultVo.ok();
        storeFreeChargeService.deleteStoreFreeCharge(storeFreecharge);
        return resultVo;
    }




    /**
     * 跳转页面：选择商品页面
     *
     * @return
     */
    @GetMapping("/toChooseCoupon")
    //@RequiresPermissions("activit:manage:chooseproduct")
    public String toChooseCoupon(Model model, Long teamId) {
        ListMemberParams listMemberParams = new ListMemberParams();
        if (getUserCentreId() != null) {
            listMemberParams.setCentreId(getUserCentreId());
        }

        Map<String, Object> map = getCentreIdModelInfo();
        List<Member> teams = commanderListService.listCommanders(listMemberParams, new Member());
        model.addAttribute("teams", teams);
        model.addAttribute("teamId", teamId);
        model.addAllAttributes(map);
        return prefix + "/chooseCoupon";
    }

}
